package com.ll.core.action;

import java.util.List;

import android.util.Log;

import com.ll.core.httpbase.HttpResponse;
import com.ll.core.httpbase.HttpUtil;
import com.ll.core.magzine.model.bean.Article;
import com.ll.core.magzine.model.bean.Period;
import com.ll.core.model.ResponseEnvelope;
import com.ll.core.util.JSONTool;
import com.ll.core.util.MagParm;
import com.ll.core.util.TypeTokens;

public class QueryAction {
	private static QueryAction queryAction;

	private QueryAction() {

	}

	public static QueryAction getInstance() {
		if (queryAction == null) {
			Class mp = MagParm.class;
			queryAction = new QueryAction();
		}
		return queryAction;
	}

	public List<Period> queryPeriodsByYear(String year) throws Exception {
		List<Period> periods = null;
		
		HttpUtil httpUtil = new HttpUtil(MagParm.REQUEST_ACTION);
		httpUtil.addParameter("year", year);
		httpUtil.addParameter("actionname", "REQ_QUERYPERIODBYYEAR");
		httpUtil.addParameter("loadimageflag", "true");
		
		HttpResponse hs = httpUtil.sendrequest();
		if (hs.getCode() == MagParm.APP_CODE_SUCCESS) {
			ResponseEnvelope<String, String> rse = hs.getResponse();
			if (rse.getAppCode() != MagParm.APP_CODE_SUCCESS) {
				throw new Exception(rse.getDetalMessage());
			} else {
				Log.d("PeriodsJSON", rse.getParameter("periods"));
				periods = (List<Period>) JSONTool.stringToObject(
						rse.getParameter("periods"),
						TypeTokens.TYPETOKEN_PERIOD);
			}
		}
		return periods;
	}
	
	public List<Article> queryArticlesByPeriod(String period) throws Exception {
		List<Article> articles = null;
		HttpUtil httpUtil = new HttpUtil(MagParm.REQUEST_ACTION);
		httpUtil.addParameter("periodId", period);
		httpUtil.addParameter("actionname", "REQ_QUERYARTICLEBYPERIOD");
		httpUtil.addParameter("loadimageflag", "true");
		
		HttpResponse hs = httpUtil.sendrequest();
		if (hs.getCode() == MagParm.APP_CODE_SUCCESS) {
			ResponseEnvelope<String, String> rse = hs.getResponse();
			if (rse.getAppCode() != MagParm.APP_CODE_SUCCESS) {
				throw new Exception(rse.getDetalMessage());
			} else {
				articles = (List<Article>) JSONTool.stringToObject(
						rse.getParameter("articles"),
						TypeTokens.TYPETOKEN_ARTICLE);
			}
		}
		return articles;
	}
}
